package cn.com.jslh.nfcpro.util;

import android.content.IntentFilter;
import android.nfc.NfcAdapter;
import android.nfc.tech.IsoDep;
import android.nfc.tech.Ndef;
import android.nfc.tech.NfcA;
import android.nfc.tech.NfcB;
import android.nfc.tech.NfcF;
import android.nfc.tech.NfcV;

/**
 * 作者：周洲 on 2016/12/1 09:30
 * <p>
 * 邮箱：double0zhou@126.com
 */

public class NfcUtil {


    // 只针对ACTION_TECH_DISCOVERED
    public static String[][] mTechList = new String[][]{
            {IsoDep.class.getName()}, {NfcA.class.getName()}, {NfcB.class.getName()},
            {NfcV.class.getName()}, {NfcF.class.getName()}, {Ndef.class.getName()}};


    public static IntentFilter[] getIntentFilter(){
        // 过滤器总共有三种
        IntentFilter intentFilter1 = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
        IntentFilter intentFilter2 = new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED);
        IntentFilter intentFilter3 = new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED);
        return new IntentFilter[]{intentFilter1, intentFilter2, intentFilter3};
    }

    public static byte[] stringToBytes(String s) {
        int len = s.length();
        byte[] data = new byte[len / 2];
        for (int i = 0; i < len; i += 2) {
            data[(i / 2)] = ((byte) ((Character.digit(s.charAt(i), 16) << 4) + Character
                    .digit(s.charAt(i + 1), 16)));
        }
        return data;
    }

    public static String bytesToString(byte[] data) {
        String temp = "";
        for (byte d : data) {
            temp += String.format("%02X", d);
        }
        return temp;
    }

    public static String flipHexStr(String s) {
        StringBuilder result = new StringBuilder();
        for (int i = 0; i <= s.length() - 2; i = i + 2) {
            result.append(new StringBuilder(s.substring(i, i + 2)).reverse());
        }
        return result.reverse().toString();
    }


    public static String ByteArrayToHexString(byte[] inarray) {
        int i, j, in;
        String[] hex = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A",
                "B", "C", "D", "E", "F"};
        String out = "";


        for (j = 0; j < inarray.length; ++j) {
            in = (int) inarray[j] & 0xff;
            i = (in >> 4) & 0x0f;
            out += hex[i];
            i = in & 0x0f;
            out += hex[i];
        }
        return out;
    }
}
